package com.chhh.cranelease.mapper;

import com.chhh.cranelease.entity.CarInfo;
import com.chhh.cranelease.entity.CraneBrand;
import com.chhh.cranelease.entity.ModelCarane;
import com.chhh.cranelease.entity.Staff;
import com.chhh.cranelease.model.CarInfoDriver;
import com.chhh.cranelease.model.DriverInfoModel;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface BaseInfoMapper {
    // 查询所有吊车
    @Select("<script>"+"select c_id,c_num,c_brand,model_name,c_create,c_buytime,c_buy,c_money,s_name from carinfo left join staff on c_people=s_card left join modelcarane on c_model=model_id  " +
            "<where> " +
            "<if test='c_num!=\"\" and s_people==\"\"'>and c_num like CONCAT('%',#{c_num},'%')</if>" +
            "<if test='s_people!=\"\" and c_num==\"\"'>and s_name like CONCAT('%',#{s_people},'%')</if>" +
            "<if test='s_people!=\"\" and c_num!=\"\"'>and  s_name like CONCAT('%',#{s_people},'%') and c_num like CONCAT('%',#{c_num},'%')</if>" +
            "<if test='s_people==\"\" and c_num==\"\"'></if>" +
            "</where>"
            +"</script>")
    List<CarInfoDriver> selectAllCarInfo(@Param("c_num") String c_num, @Param("s_people") String c_people);
    @Select("insert into carinfo (c_num,c_brand,c_model,c_create,c_buytime,c_buy,c_money,c_people) values (#{c_num},#{c_brand},#{c_model},#{c_create},#{c_buytime},#{c_buy},#{c_money},#{c_people})")
    void insertNewCar(CarInfo carInfo);

    @Update("update carinfo set c_num=#{c_num},c_brand=#{c_brand},c_model=#{c_model},c_create=#{c_create},c_buytime=#{c_buytime},c_buy=#{c_buy},c_money=#{c_money},c_people=#{c_people} where c_id=#{c_id}")
    void updateThisCar(CarInfo carInfo);

    @Delete(" <script>"+"delete from carinfo where c_id in" +
            "    <foreach collection=\"array\" item=\"id\" separator=\",\" open=\"(\" close=\")\">" +
            "        #{id}" +
            "    </foreach></script>")
    void deleteThoseCar(Integer[] c_id);


    /*// 多表查询之查出它的id 根据车的驾驶员查询所有的司机姓名
    @Select("select s_name from staff where s_card = #{c_people}")
    List<Staff> selectDriverByScard(Integer c_people);
    @Select("select model_name from modelcarane where model_id = #{c_model}")
    List<ModelCarane> selectModelByModelName(Integer c_model);*/

    // 查询所有驾驶员
    @Select("<script>"+"select s_id, s_card,s_name,s_sex,s_intime,s_outtime,s_num,s_address,state_name from staff left join staffstate on s_state=state_id " +
            "<where>" +
            "<if test='s_card!=null and s_card!=\"\"'>and s_card like CONCAT('%',#{s_card},'%')</if>" +
            "<if test='s_name!=null and s_name!=\"\"'>and s_name like CONCAT('%',#{s_name},'%')</if>" +
            "<if test='s_sex!=null and s_sex!=\"\"'>and s_sex like CONCAT('%',#{s_sex},'%')</if>" +
            "<if test='s_intime!=null and s_intime!=\"\"'>and s_intime like CONCAT('%',#{s_intime},'%')</if>" +
            "<if test='s_outtime!=null and s_outtime!=\"\"'>and s_outtime like CONCAT('%',#{s_outtime},'%')</if>" +
            "</where>"
            +"</script>")
    List<DriverInfoModel> selectAllDriver(@Param("s_card") String s_card,@Param("s_name") String s_name,@Param("s_sex") String s_sex,@Param("s_intime") String s_intime,@Param("s_outtime") String s_outtime);
    // 加入新的驾驶员
    @Insert("insert into staff (s_id,s_card,s_name,s_sex,s_num,s_address,s_state,s_intime,s_outtime)" +
            "values(#{s_id},#{s_card},#{s_name},#{s_sex},#{s_num},#{s_address},#{s_state},#{s_intime},#{s_outtime})")
    Integer insertNewDriver(Staff staff);
    @Update("update staff set s_outtime=#{s_outtime},s_card=#{s_card},s_name=#{s_name},s_sex=#{s_sex},s_num=#{s_num},s_address=#{s_address},s_state=#{s_state},s_intime=#{s_intime} where s_id = #{s_id}")
    Integer updateThisDriver(Staff staff);

    // 车型信息
    @Select("select * from modelcarane where model_id=#{model_id}")
    String selectStyle(@Param("model_id") String model_id);

    @Update("update modelcarane set images=#{surfaceName},description=#{description} where model_id = #{model_id}")
    Integer insertStyle(@Param("surfaceName") String surfaceName,@Param("model_id") Integer model_id, @Param("description") String description);

    @Select("select * from modelcarane")
    List<CraneBrand> getImage();

    @Select("select * from modelcarane where model_id=#{model_id}")
    CraneBrand getImages(@Param("model_id") Integer model_id);
}
